<?php
/**
 * Created by PhpStorm.
 * User: yandian
 * Date: 2017/4/9
 * Time: 13:31
 */

namespace app\weixin\controller;


use app\common\controller\LiteBase;
use app\common\model\Banner;
use app\common\model\CompanyInfo;
use app\common\model\Member;
use app\common\model\MemberCoupon;
use app\common\model\MemberLuck;
use app\common\util\Assert;
use app\common\util\DateTimeUtil;
use app\common\util\LoginManager;

class Index extends LiteBase
{
    public function index()
    {
        $this->assign('banners', Banner::instance()->where(['banner_type' => 0])
            ->order('sort desc')->limit(0, 5)->select());
        $this->assign('articles', \app\common\model\Article::instance()
            ->order('sort desc,id desc')
            ->limit(0, 2)
            ->select());
        return $this->fetch();
    }

    public function mine()
    {
        $this->assign('m', Member::instance()
            ->where(['id' => LoginManager::getLoginUserId()])->find());
        return $this->fetch();
    }

    public function message()
    {
        return $this->fetch();
    }

    public function info()
    {
        $this->assign('m', Member::instance()
            ->where(['id' => LoginManager::getLoginUserId()])->find());
        return $this->fetch();
    }

    public function company()
    {
        $this->assign('m', CompanyInfo::instance()
            ->where(['id' => LoginManager::getLoginUserId()])->find());
        return $this->fetch();
    }

    public function power()
    {
        $mid = LoginManager::getLoginUserId();
        $coral_count = \app\common\model\Coral::instance()
            ->where(['member_id' => $mid, 'title' => ''])
            ->count();
        $this->assign('coral_count', $coral_count);
        $this->assign('corals_count', \app\common\model\CoralReef::instance()->where(['member_id' =>
            $mid, 'title' => ''])
            ->count());
        $this->assign('coupon0_count', MemberCoupon::instance()->where(['coupon_id' => 0, 'member_id' => $mid])->count());
        $this->assign('coupon1_count', MemberCoupon::instance()->where(['coupon_id' => 1, 'member_id' => $mid])->count());
        $this->assign('love_count', \app\common\model\LoveCertificate::instance()->where(['member_id' => $mid])->count());
        $this->assign('luck_count', MemberLuck::instance()->where(['member_id' => $mid, 'status' => 0])->count());
        return $this->fetch();
    }

    public function login()
    {
        return $this->fetch();
    }

    public function saveInfo()
    {
        if (IS_POST) {
            $data = Assert::notEmpty(I('post.'), ['username', 'telephone']);
            Member::instance()->where(['id' => LoginManager::getLoginUserId()])->save($data);
            return $this->jsonSuccess();
        }
        return $this->jsonFail();
    }

    public function saveCompanyInfo()
    {
        if (IS_POST) {
            $data = Assert::notEmpty(I('post.'),
                ['title', 'corporation', 'identity', 'username', 'telephone']
            );
            $data['id'] = LoginManager::getLoginUserId();
            $data['create_time'] = DateTimeUtil::now();
            if (!CompanyInfo::instance()->where(['id' => $data['id']])->count()) {
                CompanyInfo::instance()
                    ->add($data);
            } else {
                CompanyInfo::instance()->where(['id' => $data['id']])
                    ->save($data);
            }
            return $this->jsonSuccess();
        }
        return $this->jsonFail();
    }
}